package com.antdesignbackend.backend.controller;

import com.antdesignbackend.backend.aop.AuthToken;
import com.antdesignbackend.backend.common.returnResult.ReturnVO;
import com.antdesignbackend.backend.entity.Cabinet;
import com.antdesignbackend.backend.entity.Cabinetitem;
import com.antdesignbackend.backend.entity.entityVo.CabinetVo;
import com.antdesignbackend.backend.service.CabinetService;
import com.antdesignbackend.backend.service.CabinetitemService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/cabinet")
public class CabinetController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    ReturnVO result = new ReturnVO();
    @Autowired
    CabinetService cabinetService;
    @Autowired
    CabinetitemService cabinetitemService;
    @Autowired
    InterfaceController interfaceController;
    /**
     *售货柜管理
     */
    @GetMapping("/index")
    @AuthToken
    public String index() {
        return "cabinet/index";
    }
    /**
     * 表格查询
     */
    @PostMapping("/tableSearch")
    @ResponseBody
    public ReturnVO tableSearch(@RequestBody CabinetVo  cabinetVo) throws NoSuchMethodException, SecurityException,
            IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        List<CabinetVo> cabinetVos=new ArrayList<>();
        QueryWrapper<CabinetVo> wrapper =new QueryWrapper<>();
        if(cabinetVo.getRoomId()!=null&& !cabinetVo.getRoomId().equals(""))
        {
            wrapper.eq("a.roomId",cabinetVo.getRoomId());
        }
        if(cabinetVo.getShopId()!=null&& !cabinetVo.getShopId().equals(""))
        {
            wrapper.eq("a.shopId",cabinetVo.getShopId());
        }
        if(cabinetVo.getName()!=null&& !cabinetVo.getName().equals(""))
        {
            wrapper.like("a.name",cabinetVo.getName());
        }
        //先统计满足条件的数据总数，因为sql 不包括排序
        int total=cabinetService.count(wrapper);
        result.setTotal(total);
        System.out.println("*******************************满足条件的数据有："+total);
        //再查询
        if(total>=0)
        {
            cabinetVos=cabinetService.search(wrapper,new Page<>(cabinetVo.getPageIndex(),cabinetVo.getPageSize()));
        }
        result.setData(cabinetVos);
        return result;
    }
    /**
     * 根据id删除
     */
    @DeleteMapping("/deleteById/{id}")
    @ResponseBody
    public ReturnVO deleteById(@PathVariable Serializable id) {
        result = new ReturnVO();
        if (cabinetService.removeById(Integer.parseInt(id.toString())) == true) {
            result.setMessage("删除成功");
        } else {
            result.setMessage("删除失败");
            result.setCode("0");
        }
        return result;
    }
    /**
     * 根据id开柜子自研
     */
    @GetMapping("/openById/{id}")
    @ResponseBody
    public ReturnVO openById(@PathVariable Serializable id) {
        result = new ReturnVO();
        String reulsts = "Error";
        QueryWrapper<Cabinetitem> wrapper =new QueryWrapper<>();
        wrapper.eq("cabId",id);
        List<Cabinetitem> cabinetitems=cabinetitemService.list(wrapper);
        if(cabinetitems.size()>0){
            reulsts=interfaceController.cabinetitem(cabinetitems.get(0).getCabinetitemId(),999);
        }
        if (reulsts.equals("OK")) {
            result.setMessage("打开成功！");
            result.setCode("200");
        } else {
            result.setMessage("打开失败！");
            result.setCode("500");
        }
        return result;
    }
    /**
     * 根据id开柜子大马
     */
    @GetMapping("/openByIddm/{id}")
    @ResponseBody
    public ReturnVO openByIddm(@PathVariable Serializable id) {
        result = new ReturnVO();
        String reulsts = "Error";
        QueryWrapper<Cabinetitem> wrapper =new QueryWrapper<>();
        wrapper.eq("cabId",id);
        List<Cabinetitem> cabinetitems=cabinetitemService.list(wrapper);
        if(cabinetitems.size()>0){
            reulsts=interfaceController.cabinetitemdm(cabinetitems.get(0).getCabinetitemId(),999);
        }
        if (reulsts.equals("OK")) {
            result.setMessage("打开成功！");
            result.setCode("200");
        } else {
            result.setMessage("打开失败！");
            result.setCode("500");
        }
        return result;
    }
    /**
     * 修改/新增操作的保存
     */
    @PostMapping("/save")
    @ResponseBody
    @AuthToken
    public ReturnVO save(@RequestBody Cabinet cabinet, HttpServletRequest request) throws Exception {
        result = new ReturnVO();
        boolean b = false;
        if(cabinet.getCabinetId()==0){
            b=cabinetService.save(cabinet);
        }else{
            b=cabinetService.updateById(cabinet);
        }
        if(b==true){
            result.setMessage("保存成功！");
            result.setCode("200");
        }
        else
        {
            result.setMessage("保存失败！");
            result.setCode("500");
        }
        return result;
    }
}
